﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using classes;

namespace Forms
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();

            // generate random order number
            var n = new Random();
            var num = n.Next(100000); // min=0 max=99,999
            var strNum = num.ToString();
            orderNumber.Text = "ORD-"+strNum.PadLeft(5,'0');
            orderNumber.ReadOnly = true;

            dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(myDataGridview1_CellValueChanged);
            dataGridView2.CellValueChanged += new DataGridViewCellEventHandler(myDataGridview2_CellValueChanged);
            dataGridView3.CellValueChanged += new DataGridViewCellEventHandler(myDataGridview3_CellValueChanged);
        }

        private void myDataGridview1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            double quantity, unitPrice;
            int i;
            for (i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                if (dataGridView1.Rows[i].Cells["unitPrice1"].Value != null && dataGridView1.Rows[i].Cells["quantity1"].Value != null)
                {
                    unitPrice = Convert.ToDouble(dataGridView1.Rows[i].Cells["unitPrice1"].Value.ToString());
                    quantity = Convert.ToDouble(dataGridView1.Rows[i].Cells["quantity1"].Value.ToString());
                    dataGridView1.Rows[i].Cells["totalUnitPrice1"].Value = getTotalUnitPrice(quantity, unitPrice).ToString();
                }
            }
        }

        private void myDataGridview2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            double quantity, unitPrice;
            int i;
            for (i = 0; i < (dataGridView2.Rows.Count); i++)
            {
                if (dataGridView2.Rows[i].Cells["unitPrice2"].Value != null && dataGridView2.Rows[i].Cells["quantity2"].Value != null)
                {
                    unitPrice = Convert.ToDouble(dataGridView2.Rows[i].Cells["unitPrice2"].Value.ToString());
                    quantity = Convert.ToDouble(dataGridView2.Rows[i].Cells["quantity2"].Value.ToString());
                    dataGridView2.Rows[i].Cells["totalUnitPrice2"].Value = getTotalUnitPrice(quantity, unitPrice).ToString();
                }
            }
        }

        private void myDataGridview3_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            double quantity, unitPrice;
            int i;
            for (i = 0; i < (dataGridView3.Rows.Count); i++)
            {
                if (dataGridView3.Rows[i].Cells["unitPrice3"].Value != null && dataGridView3.Rows[i].Cells["quantity3"].Value != null)
                {
                    unitPrice = Convert.ToDouble(dataGridView3.Rows[i].Cells["unitPrice3"].Value.ToString());
                    quantity = Convert.ToDouble(dataGridView3.Rows[i].Cells["quantity3"].Value.ToString());
                    dataGridView3.Rows[i].Cells["totalUnitPrice3"].Value = getTotalUnitPrice(quantity, unitPrice).ToString();
                }
            }
        }

        private double getTotalUnitPrice(double quantity, double unitPrice)
        {
            double total;
            total = quantity*unitPrice;
            return total;
        }

        private void orderFormClosed(object sender, FormClosedEventArgs e)
        {
            Program.loginForm.Show();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void projectTab1_Click(object sender, EventArgs e)
        {
                
        }

        private void saveAndCommit_Click(object sender, EventArgs e)
        {
            int i, ctr0 = 0, ctr1 = 0, ctr2 = 0, ctr3 = 0;

            if (issuedBy.Text.Trim().Length == 0 ||
                authorizedBy.Text.Trim().Length == 0 ||
                storeName.Text.Trim().Length == 0 ||
                chequeNumber.Text.Trim().Length == 0 ||
                chequeAmount.Text.Trim().Length == 0)
            {
                ctr0++;
            }

            for (i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                if (!dataGridView1.Rows[i].IsNewRow &&
                    (dataGridView1.Rows[i].Cells["item1"].Value == null ||
                    dataGridView1.Rows[i].Cells["quantity1"].Value == null ||
                    dataGridView1.Rows[i].Cells["unitPrice1"].Value == null ||
                    dataGridView1.Rows[i].Cells["totalUnitPrice1"].Value == null ||
                    (dataGridView1.Rows[i].Cells["slipReturned1"].Value == null ||
                     dataGridView1.Rows[i].Cells["slipReturned1"].Value.ToString() == "False")
                    ))
                {
                    ctr1++;
                }
            }

            for (i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                if (projectName1.Text.Trim().Length == 0 &&
                    !dataGridView1.Rows[i].IsNewRow &&
                    (dataGridView1.Rows[i].Cells["item1"].Value != null ||
                    dataGridView1.Rows[i].Cells["quantity1"].Value != null ||
                    dataGridView1.Rows[i].Cells["unitPrice1"].Value != null ||
                    dataGridView1.Rows[i].Cells["totalUnitPrice1"].Value != null ||
                    (dataGridView1.Rows[i].Cells["slipReturned1"].Value != null &&
                     dataGridView1.Rows[i].Cells["slipReturned1"].Value.ToString() != "False")
                    ))
                {
                    ctr1++;
                }
            }

            for (i = 0; i < (dataGridView2.Rows.Count); i++)
            {
                if (!dataGridView2.Rows[i].IsNewRow &&
                    (dataGridView2.Rows[i].Cells["item2"].Value == null ||
                    dataGridView2.Rows[i].Cells["quantity2"].Value == null ||
                    dataGridView2.Rows[i].Cells["unitPrice2"].Value == null ||
                    dataGridView2.Rows[i].Cells["totalUnitPrice2"].Value == null ||
                    (dataGridView2.Rows[i].Cells["slipReturned2"].Value == null ||
                     dataGridView2.Rows[i].Cells["slipReturned2"].Value.ToString() == "False")
                    ))
                {
                    ctr2++;
                }
            }

            for (i = 0; i < (dataGridView2.Rows.Count); i++)
            {
                if (projectName2.Text.Trim().Length == 0 &&
                    !dataGridView2.Rows[i].IsNewRow &&
                    (dataGridView2.Rows[i].Cells["item2"].Value != null ||
                    dataGridView2.Rows[i].Cells["quantity2"].Value != null ||
                    dataGridView2.Rows[i].Cells["unitPrice2"].Value != null ||
                    dataGridView2.Rows[i].Cells["totalUnitPrice2"].Value != null ||
                    (dataGridView2.Rows[i].Cells["slipReturned2"].Value != null &&
                     dataGridView2.Rows[i].Cells["slipReturned2"].Value.ToString() != "False")
                    ))
                {
                    ctr2++;
                }
            }

            for (i = 0; i < (dataGridView3.Rows.Count); i++)
            {
                if (!dataGridView3.Rows[i].IsNewRow &&
                    (dataGridView3.Rows[i].Cells["item3"].Value == null ||
                    dataGridView3.Rows[i].Cells["quantity3"].Value == null ||
                    dataGridView3.Rows[i].Cells["unitPrice3"].Value == null ||
                    dataGridView3.Rows[i].Cells["totalUnitPrice3"].Value == null ||
                    (dataGridView3.Rows[i].Cells["slipReturned3"].Value == null ||
                     dataGridView3.Rows[i].Cells["slipReturned3"].Value.ToString() == "False")
                    ))
                {
                    ctr3++;
                }
            }

            for (i = 0; i < (dataGridView3.Rows.Count); i++)
            {
                if (projectName3.Text.Trim().Length == 0 &&
                    !dataGridView3.Rows[i].IsNewRow &&
                    (dataGridView3.Rows[i].Cells["item3"].Value != null ||
                    dataGridView3.Rows[i].Cells["quantity3"].Value != null ||
                    dataGridView3.Rows[i].Cells["unitPrice3"].Value != null ||
                    dataGridView3.Rows[i].Cells["totalUnitPrice3"].Value != null ||
                    (dataGridView3.Rows[i].Cells["slipReturned3"].Value != null &&
                     dataGridView3.Rows[i].Cells["slipReturned3"].Value.ToString() != "False")
                    ))
                {
                    ctr3++;
                }
            }

            if (ctr0 > 0 || ctr1 > 0 || ctr2 > 0 || ctr3 > 0)
            {
                MessageBox.Show("Please answer all fields.");
            }
            else
            {
   
                var sql = "INSERT into ORDERS values('" +
                        orderNumber.Text.Trim() + "','" +
                        issuedBy.Text.Trim() + "','" +
                        orderIssueDate.Text.Trim() + "','" +
                        authorizedBy.Text.Trim() + "','" +
                        chequeNumber.Text.Trim() + "','" +
                        chequeAmount.Text.Trim() + "','" +
                        chequeIssueDate.Text.Trim() + "','" +
                        chequeDueDate.Text.Trim() + "','" +
                        storeName.Text.Trim() + "','submitted');";

                DB_sql db = new DB_sql();
                db.Insert(sql);
            }

        }

        private void saveForLater_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Form is saved for later editing.");
        }

        private void existing(string table, string id)
        {
          
        }
    }
}
